import cn.edu.dgut.Javabean_object.register;
import cn.edu.dgut.Javabean_object.userinfo;
import cn.edu.dgut.function.readfile;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/register-Servlet")
public class registerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String name = request.getParameter("name");
        String number = request.getParameter("number");
        String email = request.getParameter("email");
        String password=request.getParameter("password");
            register person=new register(username,name,number,email,password,"xx.png");
            Jsonb jsonb = JsonbBuilder.create();
            String jsonPerson = jsonb.toJson(person);


            System.out.println(jsonPerson);
            response.getWriter().println(jsonPerson);
            String info = jsonPerson.toString() + "\r\n";



           try {
               File writeName = new File("userinfo.txt"); // 相对路径，如果没有则要建立一个新的output.txt文件
               try (FileWriter writer1 = new FileWriter(writeName,true);
                    BufferedWriter out = new BufferedWriter(writer1)
               ) {
                   out.write(info);
                   out.flush(); // 把缓存区内容压入文件
                   out.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
        Map<String, String> message = new HashMap<String, String>();
        message.put("user_number", username);
        message.put("name",name);
        message.put("phone_number", number);
        message.put("email", email);
        HttpSession session = request.getSession(true);
        session.setAttribute("Login", message);

        response. sendRedirect("/userInfo.jsp");

    }
}
